<template>
  <div class="home">
    <div class="home-header">
      <a href class="home-header-logo">
        <img :src="logo" alt />
        <span>我爱报销</span>
      </a>
      <van-icon name="service-o" class="icon-ct" @click="contact" />
      <van-icon name="setting-o" class="icon-set" @click="goSet" />
    </div>
    <div class="home-userInfo">
      <van-image
        class="avatar"
        round
        fit="cover"
        :src="photo"
        @click="openEditAvatar"
      />
      <span class="name">{{ $store.state.user.nickname }}</span>
    </div>
    <div class="home-content">
      <van-tabs v-model="active" @change="tabsChange">
        <van-tab title="我的订单"></van-tab>
        <van-tab title="已完成订单"></van-tab>
      </van-tabs>
      <van-list
        v-model="listLoading"
        :finished="finished"
        :round="true"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <div class="item" v-for="(item, index) in dataList" :key="index">
          <van-cell
            :title="item.order_number"
            :value="item.status"
            @click="goDetails(item)"
          >
            <template #label>
              <span class="mr10">{{ item.service_project_title }}</span>
              <span>{{ item.expenseCate_title }}</span>
            </template>
            <template #default>
              <div>
                <van-tag
                  v-if="item.status == 11"
                  @click="goOrderEditInfo(item)"
                  round
                  type="primary"
                  >点击补充报销资料</van-tag
                >
                <span :class="item.status | statusColor" v-else>{{
                  item.status_title
                }}</span>
              </div>

              <p v-if="active == 0">
                <span v-if="item.status == 5" class="font_danger"
                  >有效时间：{{ item.invalid_cn }}分钟</span
                >
                <span v-else>{{ item.created_at }}</span>
              </p>
              <p v-else>
                <span>{{ item.updated_at }}</span>
              </p>
            </template>
          </van-cell>
        </div>
      </van-list>
    </div>

    <draggingBtn @onFloatBtnClicked="goSumbit">
      <template slot="icon">
        <van-icon name="plus" />
      </template>
    </draggingBtn>
    <editAvatar ref="editAvatar" />
    <chdialog
      ref="dialog"
      @cancel="cancel"
      @confirm="confirm"
      :confirmText="'提交订单'"
      :message="'一键提交需要绑定专利申请系统，没有绑定请选择提交订单'"
      :title="'提示'"
      :cancelText="'一键提交'"
    ></chdialog>

    <van-dialog v-model="isShowRealNameAuthentication" title="填写姓名" @confirm='RealNameSub'>
          <div class='dialog-tips'>您还没有姓名，请先填写姓名，点击确认提交！</div>
          <div class='dialog-input'>
            姓名：<input type="text" v-model="RealName" placeholder="请输入姓名" style="padding:1px 0px">
          </div>
    </van-dialog>
  </div>
</template>

<script>
import draggingBtn from "@cb/dragging-btn.vue";
import editAvatar from "@cm/a-edit-avatar.vue";
import chdialog from "../components/chdialog.vue";
import { getOrder } from "@a/order.js";
import { Dialog, Toast } from "vant";
import axios from "axios";
import config from "../../../../public/config";
import { get } from "../../../../public/API/query";

export default {
  components: {
    draggingBtn,
    editAvatar,
    chdialog
  },
  data() {
    return {
      logo: require("@/assets/img/logo.png"),
      active: 0,
      dataList: [],
      listLoading: false,
      finished: false,
      p: 1,
      status: [1, 2, 3, 5, 6, 7],
      baseUrls: process.env.VUE_APP_BASE_URL,
      originUrl: process.env.VUE_APP_ORIGIN_URL,
      A_URL: process.env.VUE_APP_A_URL,
      type: 1,
      isOATipOver: "",
      isShowRealNameAuthentication:false,
      RealName:''
    };
  },
  computed: {
    photo() {
      return ( this.$store.state.user.photo);
    }
  },
  deactivated() {
    this.$destroy(true); //销毁组件
  },
  created() {
    console.log(this.$store.state);
    // 判断是否有oa账号 给提示
    this.isOATipOver = sessionStorage.getItem("isOATipOver");
    let userinfo = JSON.parse(localStorage.getItem('userinfo'));
    if(userinfo.user.name == '' || userinfo.user.name == null){
      this.isShowRealNameAuthentication = true;
    }
  },
  methods: {
    contact() {
      Dialog.confirm({
        title: "提示",
        message: `点击确认将离开本页面跳转到手机拨号`
      })
        .then(() => {
          // on confirm
          window.location.href = "tel://13237126506";
        })
        .catch(() => {
          // on cancel
        });
    },
    cancel() {
      Toast("即将跳转到专利申请系统一键提交...");
      setTimeout(() => {
        this.$router.push("/user/set/setPatentInfo");
      }, 1500);
    },
    confirm() {
      this.$router.push("/user/apply");
    },
    goSumbit() {
      if (this.isOATipOver == "false") {
        Dialog.confirm({
          title: "提示",
          //下面格式不要更改，就是这样排列
          message: `您还没有绑定OA账号，如未绑定，则无法为您报销，请先完善资料。
<strong style='color:red;'>（注：OA账号为您的工号。例：2-XXXX)</strong>
        `,
          confirmButtonText: "立即绑定",
          //showCancelButton: false,
          confirmButtonColor: "#2ecc71",
          cancelButtonText: "取消"
        }).then(() => {
          this.$router.push("/user/set/setOAAccount");
        });
      } else {
        this.$refs.dialog.show();
      }
    },
    goSet() {
      this.$router.push("/user/set");
    },
    openEditAvatar() {
      this.$refs["editAvatar"].show();
    },
    onLoad() {
      this.listLoading = true;
      this.getData();
      this.p++;
    },
    getData(args) {
      let params = {
        p: this.p,
        type: this.type
      };
      get("ordersList", params, res => {
        if (res.code == 200) {
          res.data._list.forEach(item => {
            item.invalid_cn = Math.floor(item.invalid_cn / 60);
          });
          if (res.data._list.length === 0) {
            this.dataList = [];
            this.finished = true;
          }
          this.dataList.push(...res.data._list);
          this.listLoading = false;
          if (this.dataList.length >= res.data.count) {
            this.finished = true;
          }
        }
      });
    },
    goDetails(item) {
      this.$router.push({
        path: `/user/home/details/${item.id}`,
        params: {
          id: item.id
        }
      });
    },
    tabsChange(name) {
      switch (name) {
        case 0:
          this.dataList = [];
          this.type = 1;
          this.p = 1;
          this.onLoad();
          break;
        case 1:
          this.dataList = [];
          this.type = 2;
          this.p = 1;
          this.onLoad();
          break;
      }
    },
    goOrderEditInfo(item) {
      event.stopPropagation();
      this.$router.push({
        path: `/user/home/orderEditInfo/${item.id}`,
        params: {
          id: item.id
        }
      });
    },
    RealNameSub(){
      if(this.RealName.trim().length == 0){
        Toast.fail('请输入姓名')
      }else{
        get('realNameAuthentication',{name:this.RealName.trim()},res=>{
        if(res.code == 200){
          let userinfo = JSON.parse(localStorage.getItem('userinfo'));
          userinfo.user.name = this.RealName.trim();
          localStorage.setItem('userinfo',JSON.stringify(userinfo));
          Toast.success('提交成功,需要重新登录！');
        }
      })
      }
      
    }
  },
  filters: {}
};
</script>

<style lang="less">
.home {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  margin: auto;
  padding-top: 60px;

  &-header {
    height: 60px;
    width: 100%;
    font-size: 16px;
    // box-shadow: 0px 5px 10px 0px rgba(211, 211, 211, 0.5);
    padding: 0 10px 0 15px;
    box-sizing: border-box;
    position: fixed;
    background: #fff;
    top: 0;
    z-index: 2020;

    &-logo {
      display: inline-block;
      text-decoration: none;
      height: 100%;
      line-height: 60px;

      > img {
        width: 60px;
        height: 30px;
        object-fit: contain;
        vertical-align: middle;
        margin-right: 8px;
      }

      > span {
        font-size: 16px;
        font-weight: 800;
        color: rgba(53, 53, 53, 1);
      }
    }

    .icon-set {
      font-size: 20px;
      vertical-align: middle;
      color: #353535;
      position: absolute;
      right: 10px;
      top: 50%;
      margin-top: -9px;
    }
    .icon-ct {
      font-size: 20px;
      vertical-align: middle;
      color: #353535;
      position: absolute;
      right: 45px;
      top: 50%;
      margin-top: -9px;
    }
  }

  &-userInfo {
    height: 65px;
    width: 100%;
    background: linear-gradient(138deg, #425bff 0%, #0055eb 100%);
    position: relative;
    display: flex;
    flex-direction: row;
    align-items: center;
    box-sizing: border-box;

    .avatar {
      width: 50px;
      height: 50px;
      vertical-align: middle;
      margin: 0 10px 0 20px;
    }

    .name {
      font-size: 16px;
      font-weight: 700;
      color: #fff;
    }

    .icon-edit {
      width: 16px;
      height: 16px;
      background: url("../../../assets/img/icons/edit.png") no-repeat;
      background-size: 100%;
      position: absolute;
      bottom: 0;
      left: 45px;
    }
  }

  &-content {
    .van-tabs {
      .van-tabs__wrap {
        background: #f4f4f4;
      }

      .van-tabs__line {
        background: #1256f0;
      }
    }
  }
}
</style>

<style style="less" scoped>
.font14 {
  font-size: 14px !important;
  margin-left: 5px;
}
.van-tag {
  padding: 0.4em 1em !important;
  cursor: pointer;
}
.dialog-tips{
  font-size:0.8em;
  margin-top:3%;
  text-align: center;
  
}
.dialog-input{
  padding: 0 10%;
  margin: 6% 0;
  display: inline-block;
}
</style>
